let Base64 = require('js-base64').Base64;
import md5 from 'js-md5'
var ZHCode = {
    encrypt(txt, key='YangYang') { //加密
        var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
        var nh = Math.floor(Math.random() * 65);
        var ch = chars[nh];
        var mdKey = md5(key + ch);
        var mdKey = mdKey.substr(nh % 8, nh % 8 + 7);
        txt = Base64.encode(txt);
        var tmp = '',
            j = 0,
            k = 0;
        for (var i = 0; i < txt.length; i++) {
            k = k == mdKey.length ? 0 : k;
            j = (nh + chars.indexOf(txt[i]) + mdKey[k++].charCodeAt(0)) % 64;
            tmp += chars[j];
        }
        return encodeURI(ch + tmp);
    },
    decode(txt, key='YangYang') { //解密
        txt = decodeURI(txt);
        var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
        var ch = txt[0];
        var nh = chars.indexOf(ch);
        var mdKey = md5(key + ch);
        mdKey = mdKey.substr(nh % 8, nh % 8 + 7);
        txt = txt.substr(1);
        var tmp = '',
            j = 0,
            k = 0;
        for (var i = 0; i < txt.length; i++) {
            k = k == mdKey.length ? 0 : k;
            j = chars.indexOf(txt[i]) - nh - mdKey[k++].charCodeAt(0);
            while (j < 0) {
                j += 64;
            }
            tmp += chars[j];
        }
        return Base64.decode(tmp);
    }
}
export default ZHCode